package com.tomryan.TLabel.controller;

import com.tomryan.TLabel.config.SystemConfig;
import com.tomryan.TLabel.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import com.tomryan.TLabel.model.User;
import javax.servlet.http.HttpSession;

/**
 * Created by TangRunQing on 2018/01/30.
 */
@Controller
public class MainController {
    @Autowired
    private UserService userService;
    private final Logger logger = LoggerFactory.getLogger(MainController.class);
    /**
     * 默认首页
     *
     * @return
     */
    @RequestMapping("/")
    public String toDefaultIndex() {
        return "login";
    }

    /**
     * 后台首页
     *
     * @param model
     * @param session
     * @return
     */
    @RequestMapping(value = "index")
    public String index(Model model, HttpSession session) {
        String userId = String.valueOf(session.getAttribute(SystemConfig.SUPERUSERSESSION));
        logger.info(userId);
        if (!StringUtils.isEmpty(userId)) {
            User user = userService.selectUser(userId);
            model.addAttribute("user", user);
            return "index";
        } else {
            return "login";
        }
    }

    /**
     * 注销
     *
     * @param session
     * @return
     */
    @RequestMapping(value = "exit")
    public String exit(HttpSession session) {
        session.removeAttribute(SystemConfig.SUPERUSERSESSION);
        return "redirect:login";
    }

    /**
     * code内容
     *
     * @return
     */
    @RequestMapping(value = "code")
    public String code() {
        return "code";
    }
}
